package com.talent.intercepter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.talent.common.Const;
import com.talent.pojo.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;


/**
 * 登录拦截器
 * 
 * @author czq
 *
 */
public class LoginHandlerIntercepter implements HandlerInterceptor {

	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		String requestURI = request.getRequestURI();// 获取url地址
		// 登录是放行的
		if (requestURI.lastIndexOf("login") >= 0) {
			return true;
		}

		// 通过session判断是否登录
		HttpSession session = request.getSession();
		User currentUser = (User) session.getAttribute(Const.CURRENT_USER);
		if (currentUser != null) {
			return true;
		}

		//不符合条件的，跳转到登录界面
		request.getRequestDispatcher("/WEB-INF/views/loginUI.jsp").forward(request, response);

		return false;
	}

	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub

	}

	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub

	}

}
